package com.bsj.sipgateway.sip.session;

import com.bsj.sipgateway.cache.ExpiredHandler.impl.TransactionRedisExpiredHandler;
import com.bsj.sipgateway.cache.ExpiredTimedCache;
import com.bsj.sipgateway.command.RocketMqCommand;
import com.bsj.sipgateway.sip.bean.TransactionRedis;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sip.ClientTransaction;

@Component
public class TransactionManager {
    //封装发送设备参数    key：deviceId+port   设备id+流媒体udp端口号
    private ExpiredTimedCache<String, TransactionRedis> cache = new ExpiredTimedCache<>(20000, new TransactionRedisExpiredHandler());

    public TransactionManager(){
        cache.schedulePrune(1000);
    }

    public TransactionRedis get(String deviceId, Integer port) {
        return cache.get(deviceId + port, false);
    }

    public void put(String deviceId, Integer port, String ssrc, ClientTransaction transaction, RocketMqCommand command) {
        cache.put(deviceId + port, new TransactionRedis(transaction, ssrc, command));
    }


    public void remove(String deviceId, Integer port) {
        cache.remove(deviceId + port);
    }

}
